package token.mobileapp.controlactivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import token.mobileapp.R;
import token.mobileapp.TokenActivity;
import token.mobileapp.model.UserInfo;
import token.mobileapp.persistenceactivity.UserInfoPersistence;
import token.mobileapp.utils.Constants;
import token.mobileapp.utils.NetworkUtils;

import java.io.IOException;

/**
 * token.mobileapp.controlactivity
 * Author: AVAVT
 * Date: 8/7/11
 * Time: 10:58 PM
 */
public class AttachAccount extends TokenActivity implements View.OnClickListener {
    private Button buttonBack, buttonAttach;
    private AlertDialog alertDialog = null;
    private EditText etAccountCode;

    UserInfo userInfo = null;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.attach_account);
        buttonBack = (Button) findViewById(R.id.attach_btn_back);
        buttonBack.setOnClickListener(this);

        buttonAttach = (Button) findViewById(R.id.attach_btn_attach);
        buttonAttach.setOnClickListener(this);

        etAccountCode = (EditText) findViewById(R.id.attach_et_accountCode);

        Intent intent = new Intent(this, UserInfoPersistence.class);
        startActivityForResult(intent, Constants.REQUEST_CODE_LOAD_USER);
    }

    public void onClick(View view) {
        if (view.getId() == R.id.attach_btn_back) {
            finish();
        } else if (view.getId() == R.id.attach_btn_attach) {
            if (etAccountCode.getText().toString().compareTo("") == 0) {
                displayAlert("Please enter your account code.");
            } else if (etAccountCode.getText().toString().length() != 10) {
                displayAlert("Deformed account code.");
            } else {
                buttonAttach.setClickable(false);
                attach();
            }
        }
    }

    private void attach() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Attaching new account server...")
                .setCancelable(false);
        alertDialog = builder.create();
        alertDialog.show();

        if (userInfo != null) {
            try {
                if (NetworkUtils.attach(userInfo.getSeed(), etAccountCode.getText().toString())) {
                    alertDialog.dismiss();
                    buttonAttach.setClickable(true);
                    displayAlert("New account attached successfully.");
                } else {
                    alertDialog.dismiss();
                    displayAlert("A problem occurred while performing request. Please try again.");
                }

            } catch (IOException e) {
                alertDialog.dismiss();
                displayAlert("A problem occurred while trying to communicate with server. Please make sure you are connected to the internet and try again.");
            }
        } else {
            alertDialog.dismiss();
            displayAlert("A problem occurred while performing request. Please try again.");

            Intent loadIntent = new Intent(this, UserInfoPersistence.class);
            startActivityForResult(loadIntent, Constants.REQUEST_CODE_LOAD_USER);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == Constants.REQUEST_CODE_LOAD_USER && resultCode == Activity.RESULT_OK && data.hasExtra("userInfo")) {
            Bundle bundle = data.getExtras();
            userInfo = (UserInfo) bundle.getSerializable("userInfo");
        } else if (requestCode == Constants.REQUEST_CODE_LOAD_USER && resultCode == Activity.RESULT_CANCELED) {
            displayError(data);
        }
    }
}
